package com.example.planetandzombiedemo.entity;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.example.planetandzombiedemo.global.Config;
import com.example.planetandzombiedemo.model.BaseModel;
import com.example.planetandzombiedemo.model.Plantable;
import com.example.planetandzombiedemo.view.GameView;

public class Pea extends BaseModel implements Plantable {
    //图片帧数组
    private int frameIndex = 0;
    //标记此处是否有植物
    private int mapIndex;
    //控制发射子弹的时间戳
    private long lastBirthTime;
    //摆动速度控制
    private boolean swingSpeed;

    public Pea(int locationX, int locationY, int mapIndex) {
        this.locationX = locationX;
        this.locationY = locationY;
        this.mapIndex = mapIndex;
        isAlife = true;
        lastBirthTime = System.currentTimeMillis();
        swingSpeed = false;
    }
    @Override
    public void drawSelf(Canvas canvas, Paint paint) {
        if (isAlife) {
            //通过SwingSpeed控制显示动画帧，动画帧每2帧显示
            canvas.drawBitmap(Config.peaFrames[frameIndex],locationX,locationY,paint);
            if(!swingSpeed)
            {
                frameIndex=(++frameIndex)%8;
                swingSpeed=!swingSpeed;
            }
            else{
                swingSpeed=!swingSpeed;
            }
            //每1秒钟发射一次子弹
            if(System.currentTimeMillis()-lastBirthTime>10000)
            {
                lastBirthTime=System.currentTimeMillis();
                giveBirth2Bullet();
            }
        }
    }

    @Override
    public int getModelWidth() {
        return Config.peaFrames[0].getWidth();
    }

    @Override
    public int getMapIndex() {
        return mapIndex;
    }
    private void giveBirth2Bullet()
    {
        GameView.getInstance().giveBirth2Bullet(locationX,locationY);
    }
}
